package com.futvan.z.erp.customer;

import com.futvan.z.erp.zstorehouse.ZstorehouseService;
import com.futvan.z.erp.zstorehouse.z_storehouse_area;
import com.futvan.z.framework.common.bean.Code;
import com.futvan.z.framework.common.bean.Result;
import com.futvan.z.framework.common.service.CommonService;
import com.futvan.z.framework.core.SuperService;
import com.futvan.z.framework.core.z;
import com.futvan.z.framework.util.enums.ZxUtil;
import com.futvan.z.system.zarea.z_area;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@Service
public class CustomerService extends SuperService {

    @Resource
    private ZstorehouseService zstorehouseService;

    @Autowired
    private CommonService commonService;

    /**
     * 更新客户数据
     *
     * @param customer
     * @return
     */
    public int updateInfo(crm_customer customer) {
        int count = sqlSession.update("crm_customer_update_zid", customer);
        return count;
    }

    /**
     * 保存客户
     * @param bean
     * @param request
     * @return
     * @throws Exception
     */
    public Result crm_customer_save(@RequestParam HashMap<String, String> bean, HttpServletRequest request) throws Exception {
        Result result = new Result();
        //获取客户推荐人ID
        String parentid = bean.get("parentid");

        if (z.isNotNull(parentid)) {
//			//根据客户推荐人ID获取推荐人的网体节点序号
//			int new_node = sqlSession.selectOne("selectoneint", "SELECT node + 1 new_node FROM crm_customer WHERE zid = '"+parentid+"'");
//
//			//设置新客户的网体节点序号
//			bean.put("node", String.valueOf(new_node));

            String PageType = bean.get("PageType");
            if ("add".equals(PageType)) {
                List<crm_customer> customerInfo = getCustomerInfoByIdNumber(bean.get("zid"),bean.get("id_number"));
                if (z.isNotNull(customerInfo) && customerInfo.size() == 0) {
                    result = commonService.insertNotEvent(bean, request);
                    if (result.ifSuccess()) {
                        String customid = String.valueOf(result.getData());
                        List<crm_customer> list = sqlSession.selectList("crm_customer_select_zid", customid);
                        if (z.isNotNull(list) && list.size() == 1) {
                            ZxUtil.SendCustomerToZX("0", list.get(0));
                        }
                    }
                }
            } else if ("edit".equals(PageType)) {
                List<crm_customer> customerInfo = getCustomerInfoByIdNumber(bean.get("zid"),bean.get("id_number"));
                if (z.isNotNull(customerInfo) && customerInfo.size() == 0) {
                    result  = commonService.updateNotEvent(bean,request);
                    if (result.ifSuccess()) {
                        String customid = String.valueOf(result.getData());
                        List<crm_customer> list = sqlSession.selectList("crm_customer_select_zid", customid);
                        if (z.isNotNull(list) && list.size() == 1) {
                            ZxUtil.SendCustomerToZX("1", list.get(0));
                        }
                    }
                } else {
                    result.setCode(Code.ERROR);
                    result.setMsg("身份证号重复");
                }
            } else {
                result.setCode(Code.ERROR);
                result.setMsg("PageType is null");
            }
        } else {
            result.setCode(Code.ERROR);
            result.setMsg("推荐人不能为空");
        }


        return result;
    }

    /**
     * 根据身份证号获取用户信息
     *
     * @param IDnumber
     * @return
     */
    public List<crm_customer> getCustomerInfoByIdNumber(String customid,String IDnumber) {
        List<crm_customer> info = sqlSession.selectList("crm_customer_select_sql", "select * from crm_customer where zid!='"+customid+"' and  id_number = '" + IDnumber + "' and is_enabled = '1'");
        return info;
    }

    /**
     * 根据zid获取用户信息
     *
     * @param zid
     * @return
     */
    public crm_customer getCustomerInfo(String zid) {

        crm_customer info = sqlSession.selectOne("crm_customer_select_zid", zid);
        return info;
    }

    /**
     * 通过手机号查询用户信息
     *
     * @param tel
     * @return
     */
    public List<crm_customer> getCustomerInfoByTel(String tel) {
        List<crm_customer> info = sqlSession.selectList("crm_customer_select_sql", "select * from crm_customer where tel = '" + tel + "'");
        if (z.isNotNull(info)) {
            return info;
        }
        return null;
    }


    public crm_customer_address getAddress(String allinfo) {
        crm_customer_address address = sqlSession.selectOne("crm_customer_address_select_sql", "select * from crm_customer_address where allinfo = '" + allinfo + "'");
        if (z.isNotNull(address)) {
            z_area ar = z.areas.get(address.getAreaid());
            address.setObj(ar);
        }
        return address;
    }


    /**
     * 根据地址code获取地址信息
     *
     * @param addressid
     * @return
     */
    public crm_customer_address getCustomAddressByAddressid(String addressid) {
        crm_customer_address ca = null;

        List<crm_customer_address> address = sqlSession.selectList("crm_customer_address_select_zid", addressid);

        if (z.isNotNull(address) && address.size() == 1) {
            ca = address.get(0);
            z_area ar = z.areas.get(ca.getAreaid());
            ca.setObj(ar);
        }
        return ca;
    }

    /**
     * 根据客户ID获取地址
     *
     * @return
     */
    public crm_customer_address getCustomerAddress(String customerId, String addressid) {
        crm_customer_address ca = null;

        List<crm_customer_address> address = sqlSession.selectList("crm_customer_address_select_zid", "select * from crm_customer_address where pid = '" + customerId + "' and zid = '" + addressid + "'");

        if (z.isNotNull(address) && address.size() == 1) {
            ca = address.get(0);
            z_area ar = z.areas.get(ca.getAreaid());
            ca.setObj(ar);
        }
        return ca;
    }


    /**
     * 根据客户ID获取默认地址
     *
     * @param customid
     * @return
     */
    public crm_customer_address getCustomerDefaultAddress(String customid) {
        crm_customer_address ca = null;

        List<crm_customer_address> address = sqlSession.selectList("crm_customer_address_select_pid", customid);

        if (z.isNotNull(address) && address.size() > 0) {
            ca = address.get(0);
            z_area ar = z.areas.get(ca.getAreaid());
            ca.setObj(ar);
        }
        return ca;
    }


    /**
     * 根据地址ID获取地址
     *
     * @return
     */
    public crm_customer_address getCustomerAddressForAddressid(String addressid) {
        crm_customer_address ca = null;

        List<crm_customer_address> address = sqlSession.selectList("crm_customer_address_select_zid", addressid);

        if (z.isNotNull(address) && address.size() == 1) {
            ca = address.get(0);
            z_area ar = z.areas.get(ca.getAreaid());
            ca.setObj(ar);
        }
        return ca;
    }


    /**
     * 根据收货地址，获取发货仓库
     *
     * @return
     */
    public String getStartStorehouse(String addressid) {
        String default_storehouse_id = "";
        //判读收货ID是否为空
        if (z.isNotNull(addressid)) {
            crm_customer_address address = getCustomAddressByAddressid(addressid);
            //判读收货ID是否有效
            if (z.isNotNull(address)) {
                //获取收货地址对应省份
                String areaid = address.getAreaid().substring(0, 2) + "0000";
                //根据省份找到对应发货仓库ID
                z_storehouse_area sa = zstorehouseService.getStoreArea(areaid);
                if (z.isNotNull(sa)) {
                    default_storehouse_id = sa.getPid();
                } else {
                    default_storehouse_id = zstorehouseService.getString();
                    ;
                }
            } else {
                default_storehouse_id = zstorehouseService.getString();
                ;
            }
        } else {
            default_storehouse_id = zstorehouseService.getString();
            ;
        }
        return default_storehouse_id;
    }
}
